//生成活动海报保存到本地相册
downimageTolocal: function () {
  let = wx.getStorageSync('bmob')
  var nickName = obj.nickName; //微信昵称
  var avatarUrl = obj.avatarUrl; //微信头像
  var activeName = this.data.detail.name; //活动名称
  var activeContent = this.data.detail.content_1; //活动内容
  var activeTime = this.data.detail.time; //活动时间
  var publicTime = this.data.detail.updatedAt; //活动发布时间
  var line_num = Math.ceil((activeContent.length - 14) / 16) + 1;


  wx.showLoading({
    title: '图片生成中',
    mask: true,
  });

  let ctx = wx.createCanvasctx('mycanvas');

  ctx.beginPath();
  ctx.setFillStyle('#69c3aa'); //填充背景色
  ctx.fillRect(0, 0, 600, 300);
  ctx.setFillStyle('#f4f4f4');//填充背景色
  ctx.fillRect(0, 300, 600, 720 + line_num * 50);

  ctx.drawImage('../../../image/title.png', 190, 50, 360, 100);
  ctx.drawImage('../../../image/wave.png', 0, 200, 600, 100);

  ctx.setFillStyle("#d39705");
  ctx.setFontSize(36);
  ctx.setTextAlign('center');
  ctx.fillText(activeName, 300, 300);

  ctx.setFillStyle("#69c3aa");
  ctx.beginPath()//直线
  ctx.moveTo(50, 335);
  ctx.lineTo(550, 335);

  ctx.setFillStyle("#000");
  ctx.setFontSize(30);
  ctx.setTextAlign('left');
  ctx.fillText('尊敬的各位顾客：', 60, 380);

  ctx.setFillStyle("#000");
  ctx.setFontSize(30);
  ctx.setTextAlign('left');
  ctx.fillText(activeContent.substring(0, 14), 120, 430);
  for (var i = 0; i < line_num; i++) {
    ctx.fillText(activeContent.substring((14 + i * 16), (i * 16 + 30)), 60, (480 + 50 * i));
  }



  ctx.setFillStyle("#69c3aa");
  ctx.setFontSize(20);
  ctx.setTextAlign('right');
  ctx.fillText('发布时间：' + publicTime, 540, 450 + line_num * 50);

  ctx.setFillStyle("#69c3aa");
  ctx.moveTo(50, 480 + line_num * 50);
  ctx.lineTo(550, 480 + line_num * 50);

  ctx.drawImage('../../../image/QRcode.jpg', 60, 510 + line_num * 50, 200, 200);

  ctx.setFillStyle("#d39705");
  ctx.setFontSize(20);
  ctx.setTextAlign('center');
  ctx.fillText("人在江湖走  哪能不喝酒", 400, 585 + line_num * 50);
  ctx.fillText("👈长按扫码获取更多信息！", 400, 625 + line_num * 50);

  ctx.setFillStyle("#f00");
  ctx.setFontSize(20);
  ctx.setTextAlign('center');
  ctx.fillText("本活动最终解释权归“开心小酒”小程序店家所有", 300, 750 + line_num * 50);

  wx.downloadFile({
    url: avatarUrl,
    success: res => {
      //console.log(res);
      ctx.drawImage(res.tempFilePath, 60, 60, 80, 80);
      ctx.save();
      ctx.restore();
      ctx.stroke();


      ctx.draw(true, function (e) {
        wx.canvasToTempFilePath({
          x: 0,
          y: 0,
          width: 600,
          height: 780 + line_num * 50,
          destWidth: 1800,
          destHeight: (780 + line_num * 50) * 3,
          canvasId: 'mycanvas',
          quality: 1,
          success: function (res) {
            var tempFilePath = res.tempFilePath;
            console.log("海报图片路径：" + res.tempFilePath);
            wx.hideLoading();
            wx.showModal({
              title: '温馨提示',
              content: '活动海报已经生成，您可以保存到手机相册分享给您的朋友。',
              success: function (res) {
                if (res.confirm) {
                  console.log('用户点击确定');
                  wx.getSetting({
                    success(res) {
                      //console.log(res);
                      if (!res.authSetting['scope.writePhotosAlbum']) {
                        wx.authorize({
                          scope: 'scope.writePhotosAlbum',
                          success() {
                            //console.log('授权成功！');
                            wx.saveImageToPhotosAlbum({
                              filePath: tempFilePath,
                              success(res) {
                                wx.showToast({
                                  title: '保存成功',
                                  icon: 'success',
                                  duration: 2000
                                })
                              },
                              fail() {
                                //console.log("123");
                              }
                            });
                          },
                          fail() {
                            //console.log('授权失败！');
                          }
                        })
                      } else {
                        wx.saveImageToPhotosAlbum({
                          filePath: tempFilePath,
                          success(res) {
                            wx.showToast({
                              title: '保存成功',
                              icon: 'success',
                              duration: 2000
                            })
                          },
                          fail() {
                            //console.log("123");
                          }
                        });
                      }
                    }
                  })
                }
              }
            })
          },
          fail: function (res) {
            console.log(res);
          }
        });
      });
    }
  })
},
